import java.util.Vector;
import java.util.Random;

//holds the animal objects in a vector, this vector can be queried
//this is the handler also handles cloning new animals for no really good reason
//than its convenient and saves me from having to screw around with multiple 
//windows
//it also handles final removal of animals from the simulation

public class AnimalHandlerObject {
	//This is the handler for the animals
	//when an animal is spawned it will be added to the animal handler
	//when an animal is despawned it will be removed from the animal handler

	//this is how the render knows where the animals are
	public static boolean DEBUG = false;
	public static void setDEBUG(boolean b) {DEBUG = b;}
	public static boolean DEBUGA = false;
	public static void setDEBUGA(boolean b) {DEBUGA = b;}	
	public static boolean DEBUGB = false;
	public static void setDEBUGB(boolean b) {DEBUGB = b;}
	
	public static Vector<AnimalObject> AnimalVector = new Vector<AnimalObject>();  
		//vector to hold crap


	public static void AddAnimalToHandler(AnimalObject animal) {
		//so... we need to instance an animal and set its vars
		//... do i need two types of animal objects?
		//probably not, change that?
		if(AnimalVector.size() < 150)
			AnimalVector.add(animal);		
	}

	public static void RemoveAnimalFromHandler(AnimalObject animal) {
		AnimalVector.remove(animal);
		//animal is dead, gone, and wont come back
		//garbage collector better fucking do its thing now please
	}

	public static void updateAnimals() {
		System.out.println(AnimalVector.size());
		for(int i = 0; i < AnimalVector.size(); i++) {
			AnimalVector.get(i).decisionTree();
			if(!AnimalVector.get(i).getLiving()) {
				if(AnimalVector.get(i).getFoodValue() <= 0) {
					RemoveAnimalFromHandler(AnimalVector.get(i));
					if(DEBUG)
						System.out.println("removing animal");
				}else {
					AnimalVector.get(i).adjustFoodValue(-5);
					AnimalVector.get(i).adjustMass(-5);
					if(DEBUG)
						System.out.println("animal rots"+AnimalVector.get(i).getFoodValue()); //test
				}
					
			}

		}		
	}

	public static Vector<AnimalObject> getAnimalVector() {
		return AnimalVector;
	}
	public static void setVector(Vector<AnimalObject> V) {
		AnimalVector = V;
		//kill old threads, start new ones
	}

	public static void populationCluudge(int factor) {
		//quick and dirty, four loops, for factor*2 herbies
		//and factor carnies
		//carnies smell like cabbage

		Random rint = new Random();

		for(int i = 0;i < factor; i++ ){
			final AnimalObject me = new AnimalObject();

			me.setType(rint.nextBoolean());//test
			//me.setType(false);//test
			//me.setType(true);//test
			if(rint.nextInt(100) < 60)
				me.setSex(false); //random 0 or 1;
			else 
				me.setSex(true); //random 0 or 1;
			me.adjustOffPow(25); //offpow bonus
			me.adjustDefPow(75); //defpow bonus
			me.adjustArea(2); //+2 visible area
			//me.setMaxAge(60); //must be higher than carnivores
			me.setVelocity(1);
			me.setMaxVelocity(5);
			me.setLiving(true);
			me.setHealth(80); //our health should be something
			me.setAge(0);
			me.setMass(rint.nextInt(70)+40); //mom was REALLY preggers
			int x = rint.nextInt(512);
			int y = rint.nextInt(512);
			//System.out.println(x+","+y);//test

			me.instanceLocation();
			me.setLocation( x, y );
			//System.out.println(me.getLatitude() +","+me.getLongitude() + "||"+ x);
			me.setDisposition(0);
			//ryle: beliefs are just dispositions to behave
			//-paraphrase
			//mother.setMateable(true); //milf ahoy
			me.setMateable(0); //pedo is bad
			me.setFocus(me); //we are fixated, yup

			//so.. prepoulate the family vector with mom and dad
			//social handler will take over in a bit.

			me.setEnergy(450); //hack to stop instadeath
			AnimalHandlerObject.AddAnimalToHandler(me);  
		}
	}
}
